'use strict';
import Base from './base.js';
// 美易通信息、文章、通信类控制器请求方法
module.exports = class extends Base {
  // 请求获取客户的联系记录
  async getguestcontactlistAction() {
    const guest_id = this.get('guest_id');
    const page = this.get('page');
    const size = this.get('size');
    const nowpage = page * size;
    const info = await this.model().query(' SELECT au.name,gc.time,gc.type,gc.content FROM `think_or_guest_contact` gc LEFT JOIN `think_or_app_user` au ON gc.userid = au.id WHERE gc.guestid = ' + guest_id + ' ORDER BY gc.time desc limit ' + nowpage + ',' + size + ' ');
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '暂无联系记录...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': info });
  }
  // 请求获取网络短信模板的总分类
  async getmessagelistAction() {
    const info = await this.model().query(' SELECT * FROM `think_message_classify` WHERE pid = 0 ');
    if (think.isEmpty(info)) {
      return this.json({'code': '200', 'msg': '没有找到数据...', 'data': ''});
    } else {
      return this.json({'code': '100', 'msg': 'success...', 'data': info});
    }
  }
  // 请求获取网络短信模板二级分类
  async getmessagelistsecondAction() {
    const id = this.get('id');
    const info = await this.model().query(' SELECT * FROM `think_message_classify` WHERE pid = ' + id + ' ');
    if (think.isEmpty(info)) {
      return this.json({'code': '200', 'msg': '没有找到数据...', 'data': ''});
    } else {
      return this.json({'code': '100', 'msg': 'success...', 'data': info});
    }
  }
  // 请求获取分类下的短信列表
  async getclassmessageAction() {
    const id = this.get('id');
    const info = await this.model().query(' SELECT * FROM `think_message_demo` WHERE pid = ' + id + ' ');
    if (think.isEmpty(info)) {
      return this.json({'code': '200', 'msg': '没有找到数据...', 'data': ''});
    } else {
      return this.json({'code': '100', 'msg': 'success...', 'data': info});
    }
  }
  // 请求获取我的自定义短信模板分类
  async getmessagelistmyAction() {
    const user_id = this.get('user_id');
    const info = await this.model().query(' SELECT * FROM `think_message_classify_my` WHERE userid = ' + user_id + ' ');
    if (think.isEmpty(info)) {
      return this.json({'code': '200', 'msg': '没有找到数据...', 'data': ''});
    } else {
      return this.json({'code': '100', 'msg': 'success...', 'data': info});
    }
  }
  // 请求添加我的模板分类
  async postaddclassifymyAction() {
    const post_info = this.post('post_info');
    const addinfo = JSON.parse(post_info);
    addinfo['time'] = Math.round(Date.now() / 1000);
    const info = await this.model('message_classify_my').add(addinfo);
    if (think.isEmpty(info)) {
      return this.json({'code': '200', 'msg': '添加失败...', 'data': ''});
    } else {
      return this.json({'code': '100', 'msg': '添加成功...', 'data': info});
    }
  }
  // 请求获取我的分类下的短信模板
  async getmyclassmessageAction() {
    const id = this.get('id');
    const info = await this.model().query(' SELECT * FROM `think_message_mydemo` WHERE pid = ' + id + ' ');
    if (think.isEmpty(info)) {
      return this.json({'code': '200', 'msg': '没有找到数据...', 'data': ''});
    } else {
      return this.json({'code': '100', 'msg': 'success...', 'data': info});
    }
  }
  // 请求提交并添加我的短信模板
  async postaddmydemoAction() {
    const post_info = this.post('post_info');
    const addinfo = JSON.parse(post_info);
    addinfo['time'] = Math.round(Date.now() / 1000);
    const info = await this.model('message_mydemo').add(addinfo);
    if (think.isEmpty(info)) {
      return this.json({'code': '200', 'msg': '添加失败...', 'data': ''});
    } else {
      return this.json({'code': '100', 'msg': '添加成功...', 'data': info});
    }
  }
  // 请求并更新我的短信模板
  async postupmydemoAction() {
    const id = this.post('id');
    const content = this.post('content');
    const res = this.model('message_mydemo').where({id: id}).update({content: content});
    return think.isEmpty(res) ? this.json({ 'code': '200', 'msg': '更新失败...', 'data': '' }) : this.json({ 'code': '100', 'msg': '更新成功...', 'data': '' });
  }
  // 请求删除我的短信模板
  async postdeletemydemoAction() {
    const id = this.post('id');
    const res = this.model('message_mydemo').where({id: id}).delete();
    return think.isEmpty(res) ? this.json({ 'code': '200', 'msg': '删除失败...', 'data': '' }) : this.json({ 'code': '100', 'msg': '删除成功...', 'data': '' });
  }
  // 请求编辑更新我的分类
  async postupmyclassifyAction() {
    const id = this.post('id');
    const title = this.post('title');
    const res = this.model('message_classify_my').where({id: id}).update({title: title});
    return think.isEmpty(res) ? this.json({ 'code': '200', 'msg': '更新失败...', 'data': '' }) : this.json({ 'code': '100', 'msg': '更新成功...', 'data': '' });
  }
  // 请求删除我的短信模板分类
  async postdeletemyclassifyAction() {
    const id = this.post('id');
    const res = this.model('message_classify_my').where({id: id}).delete();
    const res1 = this.model('message_mydemo').where({pid: id}).delete();
    return think.isEmpty(res) ? this.json({ 'code': '200', 'msg': '更新失败...', 'data': '' }) : this.json({ 'code': '100', 'msg': '更新成功...', 'data': '' });
  }
  // 请求保存对客户的联系记录
  async postaddguestcontactAction() {
    const post_info = this.post('post_info');
    const addinfo = JSON.parse(post_info);
    addinfo['time'] = Math.round(Date.now() / 1000);
    const info = await this.model('or_guest_contact').add(addinfo);
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '添加失败...', 'data': '' }) : this.json({ 'code': '100', 'msg': '添加成功...', 'data': info });
  }
  // bottom
};
